/*
 * StartApplication.java
 *
 * Created on Jul 17, 2009 2:20:25 PM;
 */

package org.sunspotworld;

import com.sun.spot.core.resources.ResourceUnavailableException;
import com.sun.spot.core.resources.Resources;
import com.sun.spot.core.resources.transducers.ITriColorLED;
import com.sun.spot.core.resources.transducers.LEDColor;
import com.sun.spot.edemo.EDemoBoard;
import com.sun.spot.ieee_802_15_4_radio.IRadioPolicyManager;
import com.sun.spot.ieee_802_15_4_radio.util.IEEEAddress;

import edu.ncsu.robowulf.Robowulf;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

/**
 * The startApp method of this class is called by the VM to start the
 * application.
 * 
 * The manifest specifies this class as MIDlet-1, which means it will
 * be selected for execution.
 */
public class StartApplication extends MIDlet {

    private ITriColorLED [] leds = EDemoBoard.getInstance().getLEDs();

    protected void startApp() throws MIDletStateChangeException {
        System.out.println("Hello, world");
       // new BootloaderListener().start();   // monitor the USB (if connected) and recognize commands from host

        long ourAddr = ((IRadioPolicyManager) Resources.lookup(IRadioPolicyManager.class)).getIEEEAddress();
        System.out.println("Our radio address = " + IEEEAddress.toDottedHex(ourAddr));

        Robowulf.getInstance().setUnits(Robowulf.INCHES);
        Robowulf.getInstance().forward(12);
        Robowulf.getInstance().backward(10.5);
        Robowulf.getInstance().left(105);
        Robowulf.getInstance().right(97.2);
        LEDColor color;
        try {
            if (((Robowulf) Robowulf.getInstance()).senseForwardRange() > 10) {
                color = LEDColor.GREEN;
            } else {
                color = LEDColor.RED;
            }
        } catch (ResourceUnavailableException resourceUnavailableException) {
            color = LEDColor.TURQUOISE;
        }
        for (int i = 0; i < leds.length; i++) {
            leds[i].setColor(color);
            leds[i].setOn();
        }
        notifyDestroyed();                      // cause the MIDlet to exit
    }

    protected void pauseApp() {
        // This is not currently called by the Squawk VM
    }

    /**
     * Called if the MIDlet is terminated by the system.
     * I.e. if startApp throws any exception other than MIDletStateChangeException,
     * if the isolate running the MIDlet is killed with Isolate.exit(), or
     * if VM.stopVM() is called.
     * 
     * It is not called if MIDlet.notifyDestroyed() was called.
     *
     * @param unconditional If true when this method is called, the MIDlet must
     *    cleanup and release all resources. If false the MIDlet may throw
     *    MIDletStateChangeException  to indicate it does not want to be destroyed
     *    at this time.
     */
    protected void destroyApp(boolean unconditional) throws MIDletStateChangeException {
        for (int i = 0; i < 8; i++) {
            leds[i].setOff();
        }
    }
}
